package com.wzy.facetextview.Utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

/**
 * 保存数据基类 注：非特殊情况下，不能再单独使用 SharedPreferences原生类实例来保存数据
 * 
 */
public class BaseSharePreference {
    
    private SharedPreferences mSharePreferences;
    
    /**
     *
     * @param context
     * @param sharedFileName
     */
    protected void init(Context context, String sharedFileName) {
        
        if (TextUtils.isEmpty(sharedFileName)) {
            throw new RuntimeException("sharedFileName can't be null");
        }
        
        mSharePreferences = context.getSharedPreferences(sharedFileName, Context.MODE_PRIVATE);
        
    }
    
    public SharedPreferences getSharedPreferences() {
        
        return mSharePreferences;
    }
    
    public void putString(String key, String value) {
        
        Editor editor = mSharePreferences.edit();
        editor.putString(key, value);
        editor.commit();
    }
    
    public void putInt(String key, int value) {
        
        Editor editor = mSharePreferences.edit();
        editor.putInt(key, value);
        editor.commit();
    }
    
    public void putLong(String key, long value) {
        
        Editor editor = mSharePreferences.edit();
        editor.putLong(key, value);
        editor.commit();
    }
    
    public void putBoolean(String key, boolean value) {
        
        Editor editor = mSharePreferences.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }
    
    public String getString(String key, String defValue) {
        
        if (mSharePreferences == null) {
            throw new RuntimeException("SharedPreferences is not init", new Throwable());
        }
        return mSharePreferences.getString(key, defValue);
    }
    
    public boolean getBoolean(String key, boolean defValue) {
        
        if (mSharePreferences == null) {
            throw new RuntimeException("SharedPreferences is not init", new Throwable());
        }
        return mSharePreferences.getBoolean(key, defValue);
    }
    
    public int getInt(String key, int defValue) {
        
        if (mSharePreferences == null) {
            throw new RuntimeException("SharedPreferences is not init", new Throwable());
        }
        return mSharePreferences.getInt(key, defValue);
    }
    
    public long getLong(String key, long defValue) {
        
        if (mSharePreferences == null) {
            throw new RuntimeException("SharedPreferences is not init", new Throwable());
        }
        return mSharePreferences.getLong(key, defValue);
    }
    
    public boolean isKeyExist(String key) {
        
        if (mSharePreferences == null) {
            return false;
        }
        
        return mSharePreferences.contains(key);
    }
    
    public void remove(String key) {
        
        if (mSharePreferences == null) {
            throw new RuntimeException("SharedPreferences is not init", new Throwable());
        }
        
        Editor editor = mSharePreferences.edit();
        if (mSharePreferences.contains(key)) {
            editor.remove(key);
            editor.commit();
        }
    }
    
    public void clear() {
        
        mSharePreferences = null;
    }
}
